/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.flexengine.scene;

import com.flexengine.logger.Logger;

/**
 * @author Luann R. Athayde
 * @version 1.0
 * @since 1.0
 */
public class SceneManager {
    
    private GameScene   currentScene;
    
    private GameScene   nextScene;
    private SceneBundle sceneBundle;
    
    private final SceneBundle globalBundle;
    
    private SceneManager() {
        currentScene = new EngineIntroScene();
        nextScene = null;
        sceneBundle = new SceneBundle();
        globalBundle = new SceneBundle();
    }

    public SceneManager setNextScene(GameScene nextScene) {
        return setNextScene(nextScene, globalBundle);
    }
    
    public SceneManager setNextScene(GameScene nextScene, SceneBundle bundle) {
        this.nextScene = nextScene;
        this.sceneBundle = bundle;
        return this;
    }

    public GameScene getCurrentScene() {
        return currentScene;
    }

    public GameScene getNextScene() {
        return nextScene;
    }

    public SceneBundle getGlobalBundle() {
        return globalBundle;
    }
    
    public void changeScene() {
        if( currentScene instanceof EngineIntroScene ) {
            EngineIntroScene introScene = (EngineIntroScene) currentScene;
            if( !introScene.isDone() ) {
                return;
            }
        }
        Logger.logMessage("SceneManager - changeScene(): Changing current to ["+nextScene+"]...");
        if( currentScene!=null ) {
            currentScene.end(sceneBundle);
        }
        currentScene = nextScene;
        if( currentScene!=null ) {
            Logger.logMessage("SceneManager - changeScene(): Initializing scene...");
            currentScene.init(sceneBundle);
        }
        Logger.logMessage("SceneManager - changeScene(): Scene changed!");
    }
    
    private static SceneManager instance;
    public static SceneManager getInstance() {
        if( instance==null ) {
            instance = new SceneManager();
        }
        return instance;
    }
    
}
